﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.FrameWork;
using System.IO;
using Eis51.CampanyCMS.Business;
using Eis51.CampanyCMS.Business.Bus;
using Eis51.CampanyCMS.Model;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.UI.Admin.Baseinfo
{
    public partial class Friendadd : BasePage
    {
        private int Seqno
        {
            get
            {
                if (Request.QueryString["Seqno"] != null)
                {
                    return int.Parse(Request.QueryString["Seqno"]);
                }
                return 0;
            }

        }
 


        private string  Type
        {
            get
            {
                if (Request["Type"] != null)
                {
                    return Request["Type"];
                }
                return "F";
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShowData();
            }
        }

        private void ShowData()
        {

            Friendlink c = null;
            if (this.Seqno > 0)
            {
                c = FriendlinkBLL.GetInstance().Get(new Friendlink.Key(this.Seqno));
                if (c != null)
                {
                    UControl.CopyEntityToControl(this.Page.Form, c);
                    if (!string.IsNullOrEmpty(c.Filename))
                    {
                        imgShow.ImageUrl = string.Format("~/UpLoadFile/{0}/Img/{1}", AppContext.CurrentAdministrator.UserName, c.Filename);
                    }
                }
            }
        }

        private string SaveFile()
        {
            string sss = UploadFile.PostedFile.FileName;
            string patch = Server.MapPath(string.Format("~/UpLoadFile/{0}/Img/", AppContext.CurrentAdministrator.UserName));
            string fileName = FileListBLL.SaveFile(UploadFile, patch);
            return fileName;
        }

        private bool Save()
        {
            string filename = SaveFile();
            Friendlink c = null;
            if (this.Seqno > 0)
            {
                c = FriendlinkBLL.GetInstance().Get(new Friendlink.Key(this.Seqno));
                
            }
            else
            { 
                c =  new Friendlink () ; 
            
            }
            UControl.CopyControlToEntity(this.Page.Form, c);
           
            if (filename != string.Empty)
            {
                c.Filename = filename;
            }
            if (c.Seqno > 0)
            {

                FriendlinkBLL.GetInstance().Update(c);
            }
            else
            {
                c.Createby = AppContext.CurrentAdministrator.UserName;
                c.Licd = AppContext.LCID ;
                c.Type = this.Type;
                FriendlinkBLL.GetInstance().Insert(c);
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            Response.Redirect("FriendlistPage.aspx?Type="+this.Type);
        }
    }
}
